import 'package:flutter/cupertino.dart';
import 'package:hi_net/core/hi_base_tab_state.dart';
import 'package:hi_net/model/ranking_mo.dart';
import 'package:hi_net/model/video_model.dart';

import '../http/dio/ranking_dao.dart';
import '../model/local_data.dart';
import '../widget/video_large_card.dart';

class RankingTabPage extends StatefulWidget {
  final String sort;

  const RankingTabPage({super.key, required this.sort});

  @override
  State<RankingTabPage> createState() => _RankingTabPageState();
}

class _RankingTabPageState extends HiBaseTabState<RankingMo, VideoModel, RankingTabPage> {
  @override
  get contentChild => ListView.builder(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: const EdgeInsets.only(top: 10),
        controller: scrollController,
        itemCount: dataList.length,
        itemBuilder: (BuildContext context, int index) {
          return VideoLargeCard(videoModel: dataList[index]);
        },
      );

  @override
  Future<RankingMo> getData(int pageIndex) async {
    RankingMo result = await RankingDao.get(widget.sort, pageIndex: pageIndex, pageSize: 20);
    return result;
  }

  @override
  List<VideoModel> parseList(RankingMo result) {
    return result.list;
  }

  @override
  void loadError(loadMore) {
    super.loadError(loadMore);
    List<VideoModel>? list = getRanking(widget.sort)?.list;
    setState(() {
      if (loadMore) {
        if (list != null) {
          //合成一个新数组
          dataList = [...dataList, ...list];
          pageIndex++;
        }
      } else {
        dataList = list ?? [];
      }
    });
  }
}
